<template>
  <div class="container">
    <div class="content">
      <div class="top">
        <div class="logo"></div>
        <div class="search">
          <el-input class="input" v-model="input" size="large" :prefix-icon="Search" />
          <el-button class="search_button" @click="searchProduct()">搜索</el-button>
        </div>
      </div>
      <div class="product">
        <div v-for="(collect, index) in Collection" :key="collect.shelfId" class="list">
          <div class="title">
            <img width="210px" height="210px" :src="collect.image" />
          </div>
          <div class="detail">
            <div class="detail_top">
              <div class="name">{{ collect.name }}</div>
              <el-input-number
                class="count"
                v-model="collect.num"
                controls-position="right"
                :min="1"
                :max="10"
                size="small"
              >
                <template #decrease-icon>
                  <el-icon>
                    <Minus />
                  </el-icon>
                </template>
                <template #increase-icon>
                  <el-icon>
                    <Plus />
                  </el-icon>
                </template>
              </el-input-number>
            </div>

            <div class="clearfix detail_bottom">
              <div class="price">￥{{ collect.shelfPrice }}</div>
              <div class="car" @click="buy_product(collect.shelfId, collect.num)">
                <svg-icon name="cart" width="30px" height="30px"></svg-icon>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { allProducts, searchByName, buy } from '../api/goods'
// interface collect {
//   isCollected?: boolean
//   categoryName: string
//   deleted: number
//   productDescription: string
//   productId: number
//   productInventory: number
//   productName: string
//   productPicture: string
//   productPrice: number
// }
// const Collection: collect[] = reactive([])
const Collection: any = reactive([])
const input = ref('')

onMounted(() => {
  fetchCollection()
})

//得到商品列表
async function fetchCollection() {
  const result: any = await allProducts()
  if (result.code == 0) {
    Collection.splice(0, Collection.length, ...result.data)
    Collection.forEach((collect) => {
      collect.num = 1
    })
  } else {
    Promise.reject(new Error(result.data))
  }
}

//搜索框得到的商品列表
async function searchProduct() {
  const result: any = await searchByName(input.value)
  if (result.code == 0) {
    Collection.splice(0, Collection.length, ...result.data)
    Collection.forEach((collect) => {
      collect.num = 1
    })
    input.value = ''
  } else {
    Promise.reject(new Error(result.data))
  }
}

//添加进购物车
async function buy_product(id: string, num: number) {
  const result: any = await buy({
    shelfId: id,
    quantity: num,
  })
  if (result.code == 0) {
    ElNotification.success({ title: '成功' })
  } else {
    Promise.reject(new Error(result.data))
    ElNotification.error({ title: '失败', message: result.message })
  }
}
</script>
<style scoped lang="scss">
.clearfix::after {
  content: '';
  display: block;
  clear: both;
}
.container {
  background-color: rgba(255, 255, 255, 0.279);
  padding: 30px 0;
  font-family: Roboto;
  cursor: default;
  .content {
    padding: 10px 20px;
    color: #423d3d;
    min-height: 100vh;
    .top {
      width: 100%;
      height: 120px;
      padding: 0 100px;
      line-height: 120px;
      .logo {
        float: left;
        width: 100px;
        height: 100px;
        border-radius: 50px;
        background: url('@/assets/images/2.png') no-repeat;
        background-size: cover;
        opacity: 0.7;
      }
      .search {
        float: right;
        .input {
          width: 300px;
        }
        .search_button {
          height: 35px;
          margin: 0 5px;
        }
      }
    }
    .product {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .list {
        width: 220px;
        height: 350px;
        border: 1px solid pink;
        border-radius: 10px;
        margin: 10px;
        background-color: rgba(255, 255, 255, 0.708);
        .title {
          width: 100%;
          height: 220px;
          position: relative;
          img {
            display: block;
            margin: 5px auto;
            border-radius: 10px;
          }
        }
        .detail {
          margin: 5px 8px;
          .detail_top {
            position: relative;
            .name {
              height: 48px;
              width: 120px;
              font-size: 18px;
              line-height: 24px;
              overflow: hidden;
            }
            .count {
              width: 60px;
              position: absolute;
              top: 0;
              right: 10px;
            }
          }
          .detail_bottom {
            padding: 25px 20px 0 3px;
            position: relative;

            .price {
              color: orange;
              font-size: 22px;
            }
            .car {
              position: absolute;
              top: 20px;
              right: 10px;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
}
</style>
